<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active {
      text-decoration: none;
      color: red;
    }
  </style>
</head>

<body>
  <div id="app">

    <h2>今天开心的学路由了</h2>
    <!-- router-link组件用来切换vue路由的 -->
    <router-link to="/home">首页</router-link>
    <router-link to="/news">新闻</router-link>
    <router-link to="/about">关于我们</router-link>
    <hr>
    <!-- 定义一个 出口组件 告诉 路由组件在这里渲染 -->
    <router-view></router-view>
  </div>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>
  <script>
    // 定义三个组件对象
    const Home = {
      template: `
        <div>
          <h2>我是home页</h2>
        </div>
      `
    }
    const News = {
      template: `
        <div>
          <h2>我是新闻页</h2>
        </div>
      `
    }
    const About = {
      template: `
        <div>
          <h2>我是关于我们页</h2>
        </div>
      `
    }
    const Login = {
      template: `
        <div>
          <h2>登录页</h2>
          <button @click="doLogin">点击登录</button>
        </div>
      `,
      mounted(){
        console.log(this.$route);
      },
      methods: {
        doLogin() {
          setTimeout(() => {
            const token = 'hfief93ehc943uefih3';
            // 拿到token后要缓存
            localStorage.setItem('token', token);
            // 用户角色
            const role = "superAdmin";
            localStorage.setItem('role', role);
            // 跳转回来
            if(this.$route.query.from){
              // 没有登录拦截跳转过来
              this.$router.replace(this.$route.query.from)
            }else{
              this.$router.replace('/home')
            }
          }, 1000)
        }
      }
    }
    // 没有权限路由
    const Nopermission = {
      template: `
        <div>
          <h2>不好意思你没有权限，请联系管理员开通</h2>
        </div>
      `
    }
    // 定义路由规则（什么path对应什么组件），此时这些路由对象会注册会路由组件
    const routes = [
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home',
        component: Home,
        meta: {
          // 当前路由可以访问的用户角色有哪些
          roles: ['admin','superAdmin','worker']
        }
      },
      {
        path: '/news',
        component: News,
        meta: {
          roles: ['admin','superAdmin','worker']
        }
      },
      {
        path: '/about',
        component: About,
        meta: {
          roles: ['superAdmin']
        }
      },
      {
        path: '/login',
        name: 'login',
        meta: {
          roles: ['admin','superAdmin','worker']
        },
        component: Login
      },
      {
        path: '/noauth',
        component: Nopermission,
        meta:{ 
          roles: ['admin','superAdmin','worker']
        }
      }
    ]
    // 创建一个路由对象
    const router = new VueRouter({

      // routes定义路由规则
      routes
    })
    // 调用全局前置守卫
    router.beforeEach((to, from, next) => {
      /* 
      后台管理角色权限
        后台管理所有的路由都需要登录除了登录
      */
     console.log(to.path);
      if(to.path == '/login'){
        next();
      }else{
        // 判断是否登录
        const token = localStorage.getItem('token');
        if(token){
          // 判断当前用户角色能否访问 目标路由
          const roles = to.meta.roles;
          // 当前用户的role
          const role = localStorage.getItem('role');
          if(roles.includes(role)){
            // 当前用户可以访问
            next();
          }else{
            // 当前用户角色不能访问
            next('/noauth')
          }
          
        }else{
          // 没有登录
          next('/login')
        }
      }
    })
    // 将router对象 挂载到 vm上
    const vm = new Vue({
      el: '#app',
      router
    })
  </script>
</body>

</html>